package edu.jhu.cs.oose.scheduleit.backingbeans;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import edu.jhu.cs.oose.scheduleit.services.UserService;
import edu.jhu.cs.oose.scheduleit.services.impl.DataBaseException;
import edu.jhu.cs.oose.scheduleit.util.Attribute;
import edu.jhu.cs.oose.scheduleit.util.UserAttributeKind;
import edu.jhu.cs.oose.scheduleit.vo.User;

/**
 * the backing bean used to deal with all functions related to 'User' feature.
 * @author oose_group2
 *
 */
public class UserBackingBean{
	/**
	 * save input email
	 */
	private String email;
	/**
	 * save input password
	 */
	private String password;
	/**
	 * save input nickname
	 */
	private String nickname;
	/**
	 * save the log-in user
	 */
	private User login_user;
	/**
	 * reference of userService
	 */
	private UserService userService;
	/**
	 * Constructor of UserBackingBean
	 */
	public UserBackingBean(){
		System.out.println("new Ubb");
	}
	/**
	 * getter of login_user
	 * @return
	 */
	public User getLogin_user() {
		return login_user;
	}
	/**
	 * setter of login_user
	 * @param loginUser
	 */
	public void setLogin_user(User loginUser) {
		this.login_user = loginUser;
	}
	/**
	 * getter of userService
	 * @return
	 */
	public UserService getUserService() {
		return userService;
	}
	/**
	 * setter of userService
	 * @param userService
	 */
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	/**
	 * getter of email
	 * @return
	 */
	public String getEmail() {
		if(this.login_user!=null){
			this.logout();
		}
		return null;
	}
	/**
	 * setter of email
	 * @param email
	 */
	public void setEmail(String email) {
		this.email = email;
	}
	/**
	 * getter of password
	 * @return
	 */
	public String getPassword() {
		return password;
	}
	/**
	 * setter of password
	 * @param password
	 */
	public void setPassword(String password) {
		this.password = password;
	}
	/**
	 * getter of nickname
	 * @return
	 */
	public String getNickname() {
		return nickname;
	}
	/**
	 * setter of nickname
	 * @param nickname
	 */
	public void setNickname(String nickname) {
		this.nickname = nickname;
	}
	/**
	 * method to respond to user register
	 * @return JSF direction string
	 */
	public String register(){
		Attribute attribute = new Attribute();
		attribute.put(UserAttributeKind.EMAIL,this.email);
		attribute.put(UserAttributeKind.PASSWORD,this.password);
		attribute.put(UserAttributeKind.NICK_NAME, this.nickname);
		try {
			this.login_user = this.userService.register(attribute);
			if(this.login_user == null)return "failure";
			return "correct";
		} catch (DataBaseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "failure";
		}
	}
	/**
	 * method used to respond to user log in
	 * @return JSF direction string
	 */
	public String login(){
		System.out.println("begin login");
		User user;
		try {
			user = this.userService.login(this.email, this.password);
			if(user==null)return "failure";
			this.setLogin_user(user);
			System.out.println("Here logined:");
			return "correct";
		} catch (DataBaseException e) {
			return "failure";
		}
	}
	/**
	 * method used to respond to user log out
	 * @return JSF direction string.
	 */
	public String logout(){
		FacesContext context = FacesContext.getCurrentInstance();
		HttpSession hs = (HttpSession)context.getExternalContext().getSession(true);
		hs.invalidate();
		return "logout";
	}
}
